//
// Created by zlf on 24-1-2.
//

#include "map_decoder/map_decoder.h"

#include <iostream>

#include "generated/version.h"
#include "map_decoder/map_decoder_impl.h"

namespace autodrive {
namespace perception {

MapDecoder::MapDecoder() : map_decoder_impl_ptr_{nullptr} {}

MapDecoder::~MapDecoder() {}

void MapDecoder::Init(MapDecoderParameter const& para) noexcept(false) {
  auto initFun{[&]() {
    if (map_decoder_impl_ptr_ == nullptr) {
      map_decoder_impl_ptr_ = std::make_shared<MapDecoderImpl>();
    }
  }};
  std::call_once(init_flag_, initFun);

  try {
    map_decoder_impl_ptr_->Init(para);
  } catch (const std::exception& e) {
    throw;
  }
}

bool MapDecoder::IsInit() noexcept {
  if (map_decoder_impl_ptr_ != nullptr) {
    return map_decoder_impl_ptr_->IsInit();
  } else {
    return false;
  }
}

template <typename _ScalerType>
void MapDecoder::Decode(_ScalerType const* dpuout_cls,
                        _ScalerType const* dpuout_reg, std::uint8_t cls,
                        std::uint8_t cls_scale, std::uint8_t reg_scale,
                        Map& map) noexcept(false) {
  try {
    map_decoder_impl_ptr_->Clear();
    map_decoder_impl_ptr_->MapDecode(dpuout_cls, dpuout_reg, cls, cls_scale,
                                     reg_scale, map);
  } catch (const std::exception& e) {
    throw;
  }
}

template void MapDecoder::Decode(float const* dpuout_cls,
                                 float const* dpuout_reg, std::uint8_t cls,
                                 std::uint8_t cls_scale, std::uint8_t reg_scale,
                                 Map& map) noexcept(false);

template void MapDecoder::Decode(std::int16_t const* dpuout_cls,
                                 std::int16_t const* dpuout_reg,
                                 std::uint8_t cls, std::uint8_t cls_scale,
                                 std::uint8_t reg_scale,
                                 Map& map) noexcept(false);

template void MapDecoder::Decode(std::int8_t const* dpuout_cls,
                                 std::int8_t const* dpuout_reg,
                                 std::uint8_t cls, std::uint8_t cls_scale,
                                 std::uint8_t reg_scale,
                                 Map& map) noexcept(false);

}  // namespace perception
}  // namespace autodrive